import { ref } from 'vue'
import { updateSettlementPrice } from '@/api/order'

const useUpdatePriceForm = () => {
  const form = ref<{ orderSettlePrice: number; oldOrderSettlePrice: number }>({
    orderSettlePrice: 0,
    oldOrderSettlePrice: 0,
  })

  const submitLoading = ref(false)
  const submit = async (id: string) => {
    try {
      submitLoading.value = true
      const res = await updateSettlementPrice({
        networkModulePrice: form.value.orderSettlePrice,
        workId: id,
      })
      submitLoading.value = false
      return res.data as { code: number; data: object; message: string; success: boolean }
    } catch (error) {
      submitLoading.value = false
      return {
        code: 20000,
        data: {},
        message: '更改结算价失败，请稍后重试',
        success: false,
      }
    }
  }

  const reset = (orderSettlePrice: number) => {
    form.value.orderSettlePrice = orderSettlePrice
  }

  return {
    form,
    submitLoading,
    reset,
    submit,
  }
}

export default useUpdatePriceForm
